package cloud.estate.complaint.service.impl;

import cloud.estate.commons.bean.Complaint;
import cloud.estate.commons.handler.exception.*;
import cloud.estate.commons.mapper.ComplaintMapper;
import cloud.estate.commons.util.JsonResult;
import cloud.estate.commons.util.ResultTool;
import cloud.estate.complaint.service.ComplaintService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ComplaintServiceImpl extends ServiceImpl<ComplaintMapper, Complaint> implements ComplaintService {
    @Override
    public JsonResult addOneComplaint(Complaint complaint){
        try {
            boolean save = save(complaint);
            if (save){
                return  ResultTool.success("添加成功");
            }
            return ResultTool.fail(501,"添加失败");
        } catch (Exception e) {
            throw new AddOneException(e.getMessage());
        }
    }
    @Override
    public JsonResult deleteOneComplaint(String id){
        try {
            boolean remove = removeById(id);
            if (remove){
                return  ResultTool.success("删除成功");
            }
            return ResultTool.fail(502,"删除失败");
        } catch (Exception e) {
            throw new DealOneByIdException(e.getMessage());
        }
    }
    @Override
    public JsonResult updateOneComplaint(Complaint complaint){
        try {
            boolean update = updateById(complaint);
            if (update){
                return  ResultTool.success("更新成功");
            }
            return ResultTool.fail(503,"更新失败");
        } catch (Exception e) {
            throw new UpdateOneException(e.getMessage());
        }
    }
    @Override
    public JsonResult getOneComplaint(String id){
        try {
            Complaint one = getById(id);
            if (one != null){
                return  ResultTool.success(one);
            }
            return ResultTool.fail(504,"查询失败");
        } catch (Exception e) {
            throw new FindOneException(e.getMessage());
        }
    }
    @Override
    public JsonResult listAllComplaint(){
        try {
            List<Complaint> list = list();
            if (list != null){
                return  ResultTool.success(list);
            }
            return ResultTool.fail(505,"查询失败");
        } catch (Exception e) {
            throw new FindAllException(e.getMessage());
        }
    }
}
